package com.fit.pages;

import java.util.Collection;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.beans.BuyBean;
import com.fit.interfaces.Buy;
import com.fit.interfaces.Customer;
import com.fit.interfaces.Jewelry;
import com.fit.interfaces.Salesman;
import com.fit.interfaces.User;
import com.fit.service.Buys;
import com.fit.service.Customers;
import com.fit.service.Jewelrys;
import com.fit.service.Salesmen;

public class EntryBuy {

	@SessionState
	private User user;

	private boolean userExists;
	
	public User getUser() {
		return user;
	}
	
	Object onActivate() {
		if (!userExists) return Login.class;
		return null;
	}
	@Persist
	private String buyCustomer;
	@Persist
	private String buySalesman;
	@Persist
	private String buyJewelry;
	
	private ViewBuy viewBuyPage;
	@InjectPage
	private ViewError viewErrorPage;


	@Inject
	private Jewelrys jewelrys;
	@Inject
	private Customers customers;
	@Inject
	private Salesmen salesmen;
	@Inject
	private Buys buys;
	
	public Collection<Jewelry> getJewelrys() {
		return jewelrys.retrive();
	}

	public Collection<Customer> getCustomers() {
		return customers.retrieve();
	}

	public Collection<Salesman> geSalesmen() {
		return salesmen.retrieve();
	}

	@OnEvent(value = "submit", component = "buyInputForm")
	Object onFromSubmit() {

		this.buySalesman = "WebApp";
		this.buyCustomer = getUser().getUserName();
		
		Jewelry jewelry = null;
		Customer customer = null;
		Salesman salesman = null;

		try {
			customer = customers.retrieveSingleCustomer(getBuyCustomer());
			salesman= salesmen
					.retrieveSingleSalesmen(getBuySalesman());
			jewelry = jewelrys.retriveSingleJewelry(getBuyJewelry());
		} catch (Exception ex) {
			viewErrorPage.setPassedMessage(ex.toString());
			return viewErrorPage;
		}
		if (customer == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Customer u Map-i");
			return viewErrorPage;
		}
		if (salesman == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Salesman u Map-i");
			return viewErrorPage;
		}
		if (jewelry == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Jewelry u Map-i");
			return viewErrorPage;
		}

		Buy buyBean = new BuyBean();
		buyBean.executeBuyCreation(salesman,customer, jewelry, buys);

		this.buyJewelry = "";
		this.buySalesman = "";
		this.buyCustomer = "";
		
		return viewBuyPage;
	}
	
	
	
	public String getBuyCustomer() {
		return buyCustomer;
	}

	public void setBuyCustomer(String buyCustomer) {
		this.buyCustomer = buyCustomer;
	}

	public String getBuySalesman() {
		return buySalesman;
	}

	public void setBuySalesman(String buySalesman) {
		this.buySalesman = buySalesman;
	}

	public String getBuyJewelry() {
		return buyJewelry;
	}

	public void setBuyJewelry(String buyJewelry) {
		this.buyJewelry = buyJewelry;
	}
}
